<?php
/**
 * Created by PhpStorm.
 * User: HLG
 * Date: 2019/10/16
 * Time: 14:01
 */
namespace app\api\model;
use app\api\controller\Send;
use app\common\model\UploadFile;
use think\Request;
use think\Db;
class Machines extends BaseModel
{
    protected $resultSetType = 'collection';
    protected $table='f_machine';

    /**
     * @param $uid
     * @return array
     * 首页显示
     */
   public function index($uid){
       $user = db('user')
           ->alias('u')
           ->join('balance b','b.user_id = u.user_id','left')
           ->field('u.nickName,u.power,b.balance')
           ->where('u.user_id',$uid)
           ->find();
       $machine = db('machine')->where(array('user_id'=>$uid,'is_receive'=>0))->field('id,hube')->select();
       $arr['user'] = $user;
       $arr['machine'] = $machine;
       $status = ['code'=>1,'msg'=>'成功','data'=>$arr];
       return $status;
   }
   /**
    * 矿机
    */
   public function apparatus($uid){
       db()->startTrans();
       $dtime = time()-86400;
       $wh['create_time'] = array('let',$dtime);
       $d = db('machine')->where($wh)->delete();//删除24小时之前的币
       $login_time = db('login_time')->where('uid',$uid)->find();
       if($login_time['status'] == 0){
           $time = time();
           $user = db('user')->where(array('user_id'=>$uid))->find();
           $hour = floor(($time-$user['create_time'])/60/60);
           if($hour>24){
               $hour = 24;
           }
          for($i=0;$i<$hour;$i++){
              $dates= date("Y-m-d H:00:00", strtotime("-$i hour"));
              $list = db('machine')->where('create_time',strtotime($dates))->find();
              if($list){
                  continue;
              }
              $where['add_time'] = array('ELT',strtotime($dates));
              $sum = db('sllog')->where($where)->sum('num');
              $c = db('machine_config')->where('id',1)->find();
              $v = json_decode($c['value'],true);
              $chan = $user['power']/$sum*$v['gift'];
              $arr = [
                  'hube'=>$chan,
                  'user_id'=>$uid,
                  'create_time'=>strtotime($dates)
              ];
               $insert = db('machine')->insert($arr);
           }
           $a = [
               'login_time'=>time(),
               'status'=>1
           ];
           $u = db('login_time')->where('uid',$uid)->update($a);
       }else{
           $user = db('user')->where(array('user_id'=>$uid))->find();
           $time = time();
           $t = db('login_time')->where('uid',$uid)->find();
           $hour = floor(($time-$t['login_time'])/60/60);
           if(($time-$t['login_time'])/60/60>0 && ($time-$t['login_time'])/60/60<1){
               $hour = 1;
           }
           if($hour>24){
               $hour = 24;
           }
           for($i=0;$i<$hour;$i++){
               $dates= date("Y-m-d H:00:00", strtotime("-$i hour"));
               $list = db('machine')->where('create_time',strtotime($dates))->find();
               if($list){
                   continue;
               }
               $where['add_time'] = array('ELT',strtotime($dates));
               $sum = db('sllog')->where($where)->sum('num');
               $c = db('machine_config')->where('id',1)->find();
               $v = json_decode($c['value'],true);
               $chan = $user['power']/$sum*$v['gift'];
               $arr = [
                   'hube'=>$chan,
                   'user_id'=>$uid,
                   'create_time'=>strtotime($dates)
               ];
               $insert = db('machine')->insert($arr);
           }
           $a = [
               'login_time'=>time(),
           ];
           $u = db('login_time')->where('uid',$uid)->update($a);
       }
       if($a && $u){
           db()->commit();
           $status = ['code'=>1,'msg'=>'成功','data'=>''];
           return $status;
       }else{
           db()->rollback();
           $status = ['code'=>-1,'msg'=>'失败','data'=>''];
           return $status;
       }
   }
   /**
    * 领取
    */
   public function receive($uid,$id){
       $balance = db('balance')->where('uid',$uid)->find();
       if($balance){
           $num = db('machine')->where(array('id',$id))->find();
         $a = db('balance')->where('uid',$uid)->setInc('balance',$num['hube']);
       }else{
           $num = db('machine')->where(array('id',$id))->find();
           $arr = [
               'user_id'=>$uid,
               'balance'=>$num['hube']
           ];
           $a = db('balance')->insert($arr);
       }
       if($a){
           db('machine')->where(array('id',$id))->setField('is_receive',1);
           $status = ['code'=>1,'msg'=>'领取成功','data'=>''];
           return $status;
       }else{
           $status = ['code'=>-1,'msg'=>'领取失败','data'=>''];
           return $status;
       }
   }
}